package com.team.employment.web.admin.controller.user;

import com.team.employment.common.result.Result;
import com.team.employment.model.entity.CompanyInfo;
import com.team.employment.model.entity.UserInfo;
import com.team.employment.web.admin.custom.utils.JwtUtil;
import com.team.employment.web.admin.dto.CompanyStatutoryDto;
import com.team.employment.web.admin.dto.LoginDTO;
import com.team.employment.web.admin.dto.ResetPasswordDTO;
import com.team.employment.web.admin.service.CompanyInfoService;
import com.team.employment.web.admin.service.UserInfoService;
//import com.team.employment.web.app.dto.CompanyStatutoryDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户操作")
@RestController
@RequestMapping("/admin/userAction")
public class UserActionController {
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private CompanyInfoService companyInfoService;

    @Operation(summary = "用户登录")
    @PostMapping("login")
    public Result<UserInfo> login(@RequestBody LoginDTO loginDTO){
        UserInfo userInfo = userInfoService.login(loginDTO.getPhone(), loginDTO.getPassword());
        return Result.ok(userInfo);
    }

    @Operation(summary = "注册用户")
    @PostMapping("register")
    public Result register(@RequestBody @Valid UserInfo userInfo){
        userInfoService.register(userInfo);
        return Result.ok();
    }

    /**
     * 客户端请求缺失 Authorization 头：
     * 方法要求 @RequestHeader("Authorization")，但客户端调用时没有发送这个头
     * Swagger/Postman 测试时常见问题：
     * 使用 Swagger UI 或 Postman 测试时忘记添加 Authorization 头
     * Authorization 头格式不正确（必须包含 "Bearer " 前缀）
     * @param companyStatutoryDto
     * @return
     */
    @Operation(summary = "注册企业")
    @SecurityRequirement(name = "Authorization")
    @PostMapping("registerCompany")
    public Result registerCompany(@RequestBody @Valid CompanyStatutoryDto companyStatutoryDto,@RequestHeader("Authorization") String authHeader){

        String token = authHeader.substring(7);
        //从token中解析出用户信息
        String phone = JwtUtil.getPhoneFromToken(token);
        companyInfoService.registerCompany(companyStatutoryDto,phone);
        return Result.ok();
    }
    @Operation(summary = "发送验证码")
    @GetMapping("/sendSmsCode/{phone}")
    public Result<String> sendSmsCode(@PathVariable String phone){
        userInfoService.sendSmsCode(phone);
        return Result.ok();
    }

    @Operation(summary = "找回密码")
    @GetMapping("getPassword")
    public Result<String> resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO){
      userInfoService.resetPassword(resetPasswordDTO.getPhone(),resetPasswordDTO.getCaptcha(), resetPasswordDTO.getNewPassword());
      return Result.ok();
    }



}
